package com.yyfax.ph.personal.authority.changepassword;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.common.form.LoginLayout;
import com.yyfax.common.tools.DensityUtil;
import com.yyfax.common.tools.RegexUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.main.MainActivity;

public class ChangePasswordActivity extends BaseYYFaxActivity implements ChangePasswordPresenter.View {

	private static final String OLD_PASSWORD_CPA = "old_password_cpa";
	private static final String NEW_PASSWORD_CPA = "new_password_cpa";
	private static int LOADER_ID_MODIFY_PWD = 906;
	/**
	 * 旧密码
	 */
	@BindView(R.id.old_password_tv)
	LoginLayout mOldPwdEdit;
	/**
	 * 新密码
	 */
	@BindView(R.id.new_password_tv)
	LoginLayout mNewPwdEdit;
	/**
	 * 确定按钮
	 */
	@BindView(R.id.change_password_btn)
	Button mChangePwdBtn;

	private ChangePasswordPresenter mPresenter;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, ChangePasswordActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return new PresenterLoader<>(this, new ChangePasswordPresenter.Generator());
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
		mPresenter = (ChangePasswordPresenter) data;
		mPresenter.attach(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		getSupportLoaderManager().initLoader(LOADER_ID_MODIFY_PWD, null, this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		getSupportLoaderManager().destroyLoader(LOADER_ID_MODIFY_PWD);
	}

	@Override
	public int getContentViewRes() {
		return R.layout.change_password_activity_layout;
	}

	@Override
	public void initContentView() {
		setBtnRightVisible(false);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.change_password);

		mTitleCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, DensityUtil.dip2px(this, 18));
		mTitleCenter.setTextColor(getResources().getColor(R.color.color_262932));
	}

	@Override
	public void onBackBtnPressed() {
		super.onBackBtnPressed();
		finish();
	}

	@Override
	public void changePasswordSuccess() {
		YJPH.getInstance().onLogoutSuccess();
		showToast(R.string.find_passwrod_success_relogin);
		MainActivity.start(this, true);
		finish();
	}

	@Override
	public void changePasswordFailure(String tips) {
		showToast(tips);
	}

	@Override
	public ChangePasswordModel.ChangePwdParams getChangePwdParams() {
		ChangePasswordModel.ChangePwdParams changePwdParams = new ChangePasswordModel.ChangePwdParams();
		changePwdParams.newPassword = mNewPwdEdit.getText().toString();
		changePwdParams.password = mOldPwdEdit.getText().toString();
		return changePwdParams;
	}

	@OnClick({R.id.change_password_btn})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.change_password_btn:
				YJPH.getInstance().hideInputWindow(ChangePasswordActivity.this);//隐藏软键盘
				judgePwd();
				break;
		}
	}

	/**
	 * 判断密码格式的正确性
	 */
	private void judgePwd() {
		String oldPwd = mOldPwdEdit.getText().toString().trim();
		String newPwd = mNewPwdEdit.getText().toString().trim();
		if (TextUtils.isEmpty(oldPwd)) {
			showToast(R.string.old_pwd_can_not_null);
			return;
		}
		if (TextUtils.isEmpty(newPwd)) {
			showToast(R.string.new_pwd_can_not_null);
			return;
		}
		if (oldPwd.length() < 6) {
			showToast(R.string.please_input_6_20_old_pwd);
			return;
		}
		if (!RegexUtil.isPassword(newPwd)) {
			showToast(R.string.please_input_6_20_new_pwd);
			return;
		}
		if (oldPwd.equals(newPwd)) {
			showToast(R.string.password_hint);
			return;
		}

		/***
		 * 新密码必须是字母和数字的组合6到20位
		 */
		if (!RegexUtil.isPassword(newPwd)) {
			showToast(R.string.code_input_register_hint);
			return;
		}

		mPresenter.changePassword();
	}

	/**
	 * 保存数据
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString(OLD_PASSWORD_CPA, mOldPwdEdit.getText().toString().trim());
		outState.putString(NEW_PASSWORD_CPA, mNewPwdEdit.getText().toString().trim());
	}

	/**
	 * 数据回显
	 */
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (savedInstanceState != null) {
			mOldPwdEdit.setText(savedInstanceState.getString(OLD_PASSWORD_CPA));
			mNewPwdEdit.setText(savedInstanceState.getString(NEW_PASSWORD_CPA));
		}
	}
}